package com.zem.imos.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zem.imos.Entity.*;
import com.zem.imos.Entity.Params.ParamsAdmin;
import com.zem.imos.Entity.Params.ParamsDoctor;
import com.zem.imos.Mapper.AdminMapper;
import com.zem.imos.Mapper.DoctorMapper;
import com.zem.imos.Mapper.UserMapper;
import com.zem.imos.Util.JWT;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpServletRequest;

@Service
public class WorkerService {
    @Autowired
    DoctorMapper doctorMapper;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    UserMapper userMapper;



    /**
     * 管理员登陆
     * @author ZEM
     * @Time 2021/11/23
     * @Description 管理员登陆返回token  token 有效期30天
     */
    public RespEntity adminlogin(String username, String password)
    {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        ParamsAdmin paramsAdmin = adminMapper.selectOneAdmin(queryWrapper);
        if(paramsAdmin == null)
        {

            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"密码或用户名错误"));
            return res;
        }
        System.out.println(paramsAdmin.getRole());
        if(paramsAdmin.getRole()!=2)
        {

            RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"请使用管理员账号登陆"));
            return res;
        }
        if(paramsAdmin.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes())))
        {

            JWT jwt = new JWT();
            String token = jwt.createJwtForAdmin(paramsAdmin,30*1000*60*60*24L);
            JSONObject object = new JSONObject();
            object.put("token",token);
//            object.put("user",user);
            RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),object);
            return res;
        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"密码或用户名错误"));
        return res;
    }
    //


    /**
     * 获取登陆的用户信息
     * @author ZEM
     * @Time 2021/11/23
     * @Description 获取登陆的用户信息  有可能是管理员 也有可能能是医生
     */
    public RespEntity getworkersinfo(HttpServletRequest request)
    {
        String token = request.getHeader("token");
        Claims claims = JWT.parseJwt(token);
        if(claims != null)
        {
            String username = claims.getId();
            User user = userMapper.selectById((Long)claims.get("userId"));
            if(user.getRole().getValue()==0)
            {
                QueryWrapper<User> queryWrapper = new QueryWrapper();
                queryWrapper.eq("username",username);
                ParamsDoctor paramsDoctor = doctorMapper.selectOneDoctor(queryWrapper);
                if(paramsDoctor.getSubdepartmentName()=="药剂师")
                    paramsDoctor.setClassification("药品管理员");
                else
                    paramsDoctor.setClassification("医生");
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),paramsDoctor);
                return res;
            }
            else if(user.getRole().getValue()==2)
            {
                QueryWrapper<User> queryWrapper = new QueryWrapper();
                queryWrapper.eq("username",username);
                ParamsAdmin paramsAdmin = adminMapper.selectOneAdmin(queryWrapper);
                paramsAdmin.setClassification("admin");
                RespEntity res = new RespEntity(new RespEntity.CRespCode(200,"连接成功"),paramsAdmin);
                return res;
            }

        }
        RespEntity res = new RespEntity(new RespEntity.CRespCode(400,"获取信息失败"));
        return res;

    }


}
